import { ref, onMounted } from 'vue'
import { delCollectDel, listCollect } from '@/api/collect.js'
import { getUser } from '@/utils/auth'

export default function () {
	//数据
	const collectList = ref([])
	//取出当前登陆人的userid
	const userid = getUser()
	const collectedStatuses = ref(new Array(collectList.value.length).fill(true));
	const noCollect = ref('../../static/images/icon/collect01.png')
	const Collected = ref('../../static/images/icon/collect03.png')
	//设置层级初始值
	const leState = ref(false)

	//方法
	//将接口封装为方法
	const listCollect1 = (() => {
		// console.log(userid,'userid.value')
		listCollect({
			userId: userid
		}).then((res) => {
			console.log(res.rows, '9999')
			//对图片进行处理(转化为数组形式)
			for (let i = 0; i < res.rows.length; i++) {
				if (res.rows[i].imgUrl) {
					res.rows[i].imgUrl = JSON.parse(res.rows[i].imgUrl)
				}
				if (res.rows[i].videoUrl) {
					res.rows[i].videoUrl = JSON.parse(res.rows[i].videoUrl)
				}
			}
			collectList.value = res.rows
		})
	})
	const tapPostD = ((id) => {
		// console.log("点击了帖子详情")
		if (leState.value == false) {
			uni.navigateTo({
				url: '/pages/postDetail/index?data=' + id
			})
		}
	})
	const handleCollectClick = ((index, id, userid) => {
		// console.log(id,userid,'1111')
		delCollectDel({
			postId: id,
			userId: userid
		}).then((res) => {
			console.log(res, '取消收藏成功')
			// 点击时反转当前index的状态
			collectedStatuses.value[index] = !collectedStatuses.value[index];
			uni.setStorageSync(`已收藏帖子${id}`, 'false');
			uni.removeStorageSync(`已收藏帖子${id}`);
			//刷新列表
			listCollect1()
		})
	})
	//向外部提供东西
	return { collectList, userid, collectedStatuses, noCollect, Collected, leState, listCollect1, tapPostD, handleCollectClick }
}